package com.cloudsigns.factorymanager2.engineering.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IFinishFeedBackView;
import com.cloudsigns.factorymanager2.engineering.IView.IUploadPhotoView;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.DialogItemBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.customview.BottomListDialog;
import com.cloudsigns.factorymanager2.engineering.presenter.FinishFeedBackPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.UploadPhotoPresenter;
import com.cloudsigns.factorymanager2.engineering.utils.PhotoUtil;
import com.cloudsigns.factorymanager2.project.activity.AddCompletionOfEvaluationActivity;
import com.cloudsigns.factorymanager2.project.activity.AddEngineeringInstallationOrderActivity;
import com.cloudsigns.factorymanager2.project.activity.CompletionOfEvaluationActivity;
import com.cloudsigns.factorymanager2.project.adapter.ProductInformationAdapter;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 完工单
 * Created by tony on 2018/4/6.
 */

public class CompletionEvaluationForEngineeringInstallFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener, BottomListDialog.OnItemDialogClick, IFinishFeedBackView, IUploadPhotoView {
    private final String TAG = CompletionEvaluationForEngineeringInstallFragment.class.getSimpleName();
    private TextView tv_project_name, tv_engineering_name;
    private GridView gridview_completion, gridview_texie_photo, gridview__other_photo;
    private View rl_completion_of_evaluation;
    private TextView btn_submit;
    private boolean isclickable;
    private GridAdapter gridview_completiondapter, gridview_texie_photoadapter, gridview__other_photoadapter;
    private List<String> imgPathList = new ArrayList<>();
    private List<String> otherImgPathList = new ArrayList<>();
    private List<String> texieImgPathList = new ArrayList<>();

    private List<String> serverimgUrlListList = new ArrayList<>();
    private List<String> serverortherimgUrlListList = new ArrayList<>();
    private List<String> servertexieimgUrlListList = new ArrayList<>();
    private final int PHOTO_COMPLETION = 0;
    private final int PHOTO_OTHER = 1;
    private final int PHOTO_TEXIE = 2;
    private int photo_current = -1;
    private ListView lv;
    private ProductInformationAdapter adapter;
    private List<ProductBean> productBeanList = new ArrayList<>();
    private FinishFeedBackPresenter finishFeedBackPresenter;
    EngineeringInstallationOrderBean engineeringInstallationOrderBean;
    private String filePath;
    private UploadPhotoPresenter uploadPhotoPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        finishFeedBackPresenter = new FinishFeedBackPresenter(this);
        uploadPhotoPresenter = new UploadPhotoPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_completion_evaluation_for_engineering_install, null);
        rl_completion_of_evaluation = view.findViewById(R.id.rl_completion_of_evaluation);

        tv_project_name = view.findViewById(R.id.tv_project_name);
        tv_engineering_name = view.findViewById(R.id.tv_engineering_name);
        btn_submit = view.findViewById(R.id.btn_submit);

        imgPathList.add("paizhao");
        gridview_completion = view.findViewById(R.id.gridview_completion);
        gridview_completion.setNumColumns(3);
        gridview_completiondapter = new GridAdapter(getActivity());
        gridview_completiondapter.setData(imgPathList);
        gridview_completion.setAdapter(gridview_completiondapter);

        texieImgPathList.add("paizhao");
        gridview_texie_photo = view.findViewById(R.id.gridview_texie_photo);
        gridview_texie_photo.setNumColumns(3);
        gridview_texie_photoadapter = new GridAdapter(getActivity());
        gridview_texie_photoadapter.setData(texieImgPathList);
        gridview_texie_photo.setAdapter(gridview_texie_photoadapter);


        otherImgPathList.add("paizhao");
        gridview__other_photo = view.findViewById(R.id.gridview__other_photo);
        gridview__other_photo.setNumColumns(3);
        gridview__other_photoadapter = new GridAdapter(getActivity());
        gridview__other_photoadapter.setData(otherImgPathList);
        gridview__other_photo.setAdapter(gridview__other_photoadapter);


        rl_completion_of_evaluation.setOnClickListener(this);
        gridview_completion.setOnItemClickListener(this);
        gridview__other_photo.setOnItemClickListener(this);
        gridview_texie_photo.setOnItemClickListener(this);
        btn_submit.setOnClickListener(this);

        btnClickable(false);
        return view;
    }

    private void submitEvalution() {
        finishFeedBackPresenter.submitFinishFeedBack();
    }

    private void btnClickable(boolean bool) {
        isclickable = bool;
        if (bool) {
            btn_submit.setBackgroundResource(R.mipmap.btn_long_bg_green);
            btn_submit.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        } else {

            btn_submit.setBackgroundResource(R.mipmap.btn_bg_short_gray);
            btn_submit.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        }
    }


    public void initData(EngineeringInstallationOrderBean engineeringInstallationOrderBean) {
        if (null != engineeringInstallationOrderBean) {
            this.engineeringInstallationOrderBean = engineeringInstallationOrderBean;
        }

        if (null != engineeringInstallationOrderBean) {
            tv_project_name.setText(engineeringInstallationOrderBean.getProjectName());
            tv_engineering_name.setText("/" + engineeringInstallationOrderBean.getEngineeringName());

            CommonEnum.AssignStatus assignStatus = engineeringInstallationOrderBean.getAssignStatus();
            if (null != assignStatus) {
                if (assignStatus == CommonEnum.AssignStatus.CREATED) {
                    btnClickable(false);
                } else if (assignStatus == CommonEnum.AssignStatus.EDITED) {
                    btnClickable(false);
                } else if (assignStatus == CommonEnum.AssignStatus.SEND) {
                    btnClickable(false);
                }
//                else if (assignStatus == CommonEnum.AssignStatus.RECEIVED) {
//                    btnClickable(true);
//                } else if (assignStatus == CommonEnum.AssignStatus.RETURNED) {
//                    btnClickable(false);
//                } else if (assignStatus == CommonEnum.AssignStatus.COMPLETED) {
//                    btnClickable(false);
//                }
            }

            CommonEnum.CompleteStatus completeStatus = engineeringInstallationOrderBean.getCompleteStatus();
            if (null != completeStatus) {
                if (completeStatus == CommonEnum.CompleteStatus.WORKING && engineeringInstallationOrderBean.getAssignStatus() == CommonEnum.AssignStatus.RECEIVED) {
                    btnClickable(true);
                } else if (completeStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                    btnClickable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                    btnClickable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE) {
                    btnClickable(false);
                }
            }


            List<String> completeImageList = engineeringInstallationOrderBean.getCompleteImageList();
            if (null != completeImageList) {
                imgPathList.clear();
                imgPathList.addAll(completeImageList);
                gridview_completiondapter.setData(imgPathList);
                gridview_completiondapter.notifyDataSetChanged();
            }

            List<String> completeOtherImageList = engineeringInstallationOrderBean.getCompleteOtherImageList();
            if (null != completeOtherImageList) {
                otherImgPathList.clear();
                otherImgPathList.addAll(completeOtherImageList);
                gridview__other_photoadapter.setData(otherImgPathList);
                gridview__other_photoadapter.notifyDataSetChanged();
            }

            List<String> completeCloseUpImageList = engineeringInstallationOrderBean.getCompleteCloseUpImageList();
            if (null != completeCloseUpImageList) {
                texieImgPathList.clear();
                texieImgPathList.addAll(completeCloseUpImageList);
                gridview_texie_photoadapter.setData(texieImgPathList);
                gridview_texie_photoadapter.notifyDataSetChanged();
            }


        }
    }


    private void showListDialog() {
        DialogItemBean[] items = new DialogItemBean[]{
                new DialogItemBean(R.string.camera,
                        R.layout.dialog_list_item_normal),
                new DialogItemBean(R.string.gallary,
                        R.layout.dialog_list_item_normal),
                new DialogItemBean(R.string.cancel,
                        R.layout.dialog_list_item_normal)};
        BottomListDialog dialog = new BottomListDialog(getActivity(), Arrays.asList(items));
        dialog.setItemClick(this);
        dialog.show();
    }


    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_completion_of_evaluation:
                mIntent = new Intent(getActivity(), AddCompletionOfEvaluationActivity.class);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;
            case R.id.btn_submit:
                if (isclickable) {
                    btnClickable(false);
                    submitEvalution();
                }

                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
        if (parent == gridview_completion) {
            photo_current = PHOTO_COMPLETION;
        } else if (parent == gridview__other_photo) {
            photo_current = PHOTO_OTHER;
        } else if (parent == gridview_texie_photo) {
            photo_current = PHOTO_TEXIE;
        }
        String imgs = (String) parent.getItemAtPosition(position);
        if ("paizhao".equals(imgs)) {
            showListDialog();
        } else {
//                    Toast.makeText(MainActivity.this, "1" + position, Toast.LENGTH_SHORT).show();
//                    PhotoPreviewIntent intent = new PhotoPreviewIntent(MainActivity.this);
//                    intent.setCurrentItem(position);
//                    intent.setPhotoPaths(imagePaths);
//                    startActivityForResult(intent, REQUEST_PREVIEW_CODE);
        }
    }

    @Override
    public void onItemDialogClick(DialogItemBean item) {

        switch (item.getmTextId()) {
            case R.string.camera: {

                PhotoUtil.startCamear(null, CompletionEvaluationForEngineeringInstallFragment.this);
                break;
            }
            case R.string.gallary: {
                PhotoUtil.startGallery(null, CompletionEvaluationForEngineeringInstallFragment.this);
                break;
            }
            default:
                break;
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
                    boolean bool = data.getBooleanExtra(ConstantUtil.COMPLETION_OF_EVALUATION_BEAN, false);
                    if (bool) {
//                        productBeanList.add(new ProductBean(null, 0, null, null, 0, null));
//                        adapter.setData(productBeanList);
//                        adapter.notifyDataSetChanged();
                    }

                }
                break;
            case PhotoUtil.CAMERA:
                if (resultCode == getActivity().RESULT_OK) {
                    File picture = new File(PhotoUtil.filePath);
                    if (!picture.exists()) {
                        picture = new File(PhotoUtil.filePath);
                    }

                    filePath = PhotoUtil.CAMERA_SEVER + picture.getName();
                    filePath = PhotoUtil.imageCompressionAndSave(picture.getAbsolutePath(), filePath);
                    //override(100, 100)

                    if (photo_current == PHOTO_COMPLETION) {
                        if (imgPathList.contains("paizhao")) {
                            imgPathList.remove("paizhao");
                        }
                        if (imgPathList.size() < 3) {
                            imgPathList.add(filePath);
                            if (imgPathList.size() < 3)
                                imgPathList.add("paizhao");

                            gridview_completiondapter.setData(imgPathList);
                            gridview_completiondapter.notifyDataSetChanged();
                        }

                    } else if (photo_current == PHOTO_OTHER) {
                        if (otherImgPathList.contains("paizhao")) {
                            otherImgPathList.remove("paizhao");
                        }
                        if (otherImgPathList.size() < 3) {
                            otherImgPathList.add(filePath);
                            if (otherImgPathList.size() < 3)
                                otherImgPathList.add("paizhao");

                            gridview__other_photoadapter.setData(otherImgPathList);
                            gridview__other_photoadapter.notifyDataSetChanged();
                        }

                    } else if (photo_current == PHOTO_TEXIE) {
                        if (texieImgPathList.contains("paizhao")) {
                            texieImgPathList.remove("paizhao");
                        }
                        if (texieImgPathList.size() < 3) {
                            texieImgPathList.add(filePath);
                            if (texieImgPathList.size() < 3)
                                texieImgPathList.add("paizhao");

                            gridview_texie_photoadapter.setData(texieImgPathList);
                            gridview_texie_photoadapter.notifyDataSetChanged();
                        }
                    }


                    uploadPhotoPresenter.uploadPhtoto();
                }


                break;
            case PhotoUtil.GALLERY:
                if (resultCode == getActivity().RESULT_OK) {

                    String path = FileUtil.getRealPathFromURI(getActivity(), Uri.parse(data.getData() + ""));

                    if (photo_current == PHOTO_COMPLETION) {
                        if (imgPathList.contains("paizhao")) {
                            imgPathList.remove("paizhao");
                        }

                        if (imgPathList.size() < 3) {

                            imgPathList.add(path);
                            if (imgPathList.size() < 3)
                                imgPathList.add("paizhao");
                            gridview_completiondapter.setData(imgPathList);
                            gridview_completiondapter.notifyDataSetChanged();
                        }


                    } else if (photo_current == PHOTO_OTHER) {
                        if (otherImgPathList.contains("paizhao")) {
                            otherImgPathList.remove("paizhao");
                        }

                        if (otherImgPathList.size() < 3) {

                            otherImgPathList.add(path);
                            if (otherImgPathList.size() < 3)
                                otherImgPathList.add("paizhao");
                            gridview__other_photoadapter.setData(otherImgPathList);
                            gridview__other_photoadapter.notifyDataSetChanged();
                        }


                    } else if (photo_current == PHOTO_TEXIE) {
                        if (texieImgPathList.contains("paizhao")) {
                            texieImgPathList.remove("paizhao");
                        }

                        if (texieImgPathList.size() < 3) {

                            texieImgPathList.add(path);
                            if (texieImgPathList.size() < 3)
                                texieImgPathList.add("paizhao");
                            gridview_texie_photoadapter.setData(texieImgPathList);
                            gridview_texie_photoadapter.notifyDataSetChanged();
                        }
                    }


                    File f = new File(path);
                    filePath = PhotoUtil.CAMERA_SEVER + f.getName();
                    filePath = PhotoUtil.imageCompressionAndSave(path, filePath);

                    uploadPhotoPresenter.uploadPhtoto();


                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public String getOrderId() {
        if (null != engineeringInstallationOrderBean)
            return engineeringInstallationOrderBean.getId();
        return "";
    }


    @Override
    public String getItemId() {
        return "";
    }


    @Override
    public CommonEnum.EngineeringORderCategory getType() {
        return CommonEnum.EngineeringORderCategory.INSTALL;
    }

    @Override
    public String getcompleteConten() {
        return "";
    }

    @Override
    public List<String> getcompleteCloseUpImageList() {
        return servertexieimgUrlListList;
    }

    @Override
    public List<String> getimgUrlList() {
        return serverimgUrlListList;
    }


    @Override
    public List<String> getOtherImgUrlList() {
        return serverortherimgUrlListList;
    }


    @Override
    public void submitFinishFeedBackSuccessed() {
        Toast.makeText(getActivity(), "提交完工单成功！", Toast.LENGTH_SHORT).show();
        btnClickable(false);
    }

    @Override
    public void submitFinishFeedBackFailed(String desc) {
        Toast.makeText(getActivity(), desc, Toast.LENGTH_SHORT).show();
        btnClickable(true);
    }

    @Override
    public String getFilePath() {

        return filePath;
    }

    @Override
    public void upLoadPhotoSuccessed(String uri) {
        if (photo_current == PHOTO_COMPLETION) {
            serverimgUrlListList.add(uri);

        } else if (photo_current == PHOTO_OTHER) {
            serverortherimgUrlListList.add(uri);
        } else if (photo_current == PHOTO_TEXIE) {
            servertexieimgUrlListList.add(uri);
        }
    }

    @Override
    public void upLoadPhotoFailed(String desc) {
        Toast.makeText(getActivity(), desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }


}
